//
//  TextTitleView.swift
//  FuXingApp
//
//  Created by zqw on 2021/9/29.
//

import UIKit
/// 导航栏titleview，居中显示（解决多字不居中的问题）
class TextTitleView: UIView {
    var title: String? {
        willSet {
            titleLabel.text = newValue
        }
    }
    var titleFont = Font16
    var titleColor: UIColor? = .black {
        willSet {
            titleLabel.textColor = newValue
        }
    }
    
    /*
     navigationItem.titleView适配，不会挤到中间 iOS11之前默认不开启自动布局，
     iOS11之后模块打开了，所以原来用frame做的自定义view，需要实现intrinsicContentSize方法，
     但是2边还是有点间隙，可以设置偏移达到效果
     */
    
    override var intrinsicContentSize: CGSize {
        return UIView.layoutFittingExpandedSize
    }
    // MARK: - init
    init(frame: CGRect,title: String,titleFont: UIFont,titleColor: UIColor) {
        self.title = title
        self.titleFont = titleFont
        self.titleColor = titleColor
        super.init(frame: frame)
        setup()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///设置
    private func setup() {
        autoresizingMask = [UIView.AutoresizingMask.flexibleTopMargin,UIView.AutoresizingMask.flexibleBottomMargin]
        autoresizesSubviews = true
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { m in
            m.edges.equalToSuperview()
        }
    }
    
    
    // MARK: lazy loads
    
    ///title
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = titleFont
        label.numberOfLines = 1
        label.text = title
        label.textColor = titleColor
        label.textAlignment = .center
        label.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,UIView.AutoresizingMask.flexibleHeight]
        return label
    }()
}
